﻿using Xejen.Ipc.Framework.Controllers;
using Xejen.Ipc.Framework.Extensions;
using Xejen.Ipc.Framework.Flows;
using Xejen.Ipc.Framework.Steps;
using Xejen.Logger;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Text;

namespace Xejen.Ipc.Framework
{
    /// <summary>
    /// 针对<see cref="IIpcHost"/>进行扩展
    /// </summary>
    /// <creator>marc</creator>
    public static class IpcHostExtensions
    {
        private static readonly ILogger _logger;

        static IpcHostExtensions()
        {
            _logger = HostRuntime.LoggerManager.CreateLogger(nameof(IpcHostExtensions));
        }

        /// <inheritdoc cref="IBuilderFactory{TData}"/>
        private class BuilderFactory<TData> : IBuilderFactory<TData>
            where TData : new()
        {
            /// <inheritdoc cref="BuilderFactory{TData}"/>
            public BuilderFactory() { }
            /// <inheritdoc/>
            public IControllerBuilder<TData> NewControllerBuilder()
            {
                return new ControllerBuilder<TData>();
            }
        }

        /// <summary>
        /// 创建工业控制流程
        /// </summary>
        /// <param name="host"><inheritdoc cref="IpcHost" path="/summary"/></param>
        /// <param name="action">配置主机流程</param>
        /// <typeparam name="TData"><inheritdoc cref="IFlow{TData}" path="/typeparam[@name='TData']"/></typeparam>
        /// <returns></returns>
        public static IFlow<TData> CreateFlowWithFactory<TData>(this IIpcHost host, Func<IBuilderFactory<TData>, IFlow<TData>> action) where TData : new()
        {
            Check.NotNull(action, nameof(action));

            _logger.LogInformation($"开始流程构建");

            BuilderFactory<TData> factory = new BuilderFactory<TData>();
            var flow = action.Invoke(factory);

            _logger.LogInformation($"流程构建完毕！流程Id:{flow.Id}，流程Name:{flow.Name}，一共 {flow.Controllers.Count} 个流程控制器");

            return flow;
        }

        /// <summary>
        /// 创建工业控制流程
        /// </summary>
        /// <param name="host"><inheritdoc cref="IpcHost" path="/summary"/></param>
        /// <param name="action">配置主机流程</param>
        /// <typeparam name="TData"><inheritdoc cref="IFlow{TData}" path="/typeparam[@name='TData']"/></typeparam>
        /// <returns></returns>
        public static IFlow<TData> CreateFlowUsingBuilder<TData>(this IIpcHost host, Func<IFlowBuilder<TData>, IFlow<TData>> action) where TData : new()
        {
            Check.NotNull(action, nameof(action));

            _logger.LogInformation($"开始流程构建");

            FlowBuilder<TData> builder = new FlowBuilder<TData>(host);
            var flow = action.Invoke(builder);

            _logger.LogInformation($"流程构建完毕！流程Id:{flow.Id}，流程Name:{flow.Name}，一共 {flow.Controllers.Count} 个流程控制器");

            return flow;
        }
    }

    /// <summary>
    /// 表示构建器工厂
    /// </summary>
    /// <typeparam name="TData"><inheritdoc cref="IFlow{TData}" path="/typeparam[@name='TData']"/></typeparam>
    public interface IBuilderFactory<TData>
        where TData : new()
    {
        /// <summary>
        /// 创建流程控制器的构建器对象
        /// </summary>
        /// <returns></returns>
        IControllerBuilder<TData> NewControllerBuilder();
    }
}
